package me.algo.lst;

import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.time.temporal.ChronoUnit.NANOS;

/**
 * @author guozheng
 * @date 2024/4/26
 */
public class Test1 {
    public static void main(String[] args) {
        List<String> list = Stream.iterate(1, i -> i + 1).limit(65535121).map(String::valueOf).collect(Collectors.toList());
        toIntList1(list);
        toIntList2(list);

        HashMap<Integer, Integer> map = new HashMap<>();
//        toIntList3(list);
    }

    public static List<Integer> toIntList1(List<String> data) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        List<Integer> rtn = Lists.newArrayListWithCapacity(data.size());
        for (String d : data) {
            rtn.add(Integer.valueOf(d));
        }
        Duration duration = stopwatch.elapsed();
        System.out.println(duration.get(NANOS));
        return rtn;
    }

    public static List<Integer> toIntList2(List<String> data) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        List<Integer> rtn = data.stream()
                .map(Integer::valueOf)
                .collect(Collectors.toCollection(() -> Lists.newArrayListWithCapacity(data.size())));
        Duration duration = stopwatch.elapsed();
        System.out.println(duration.get(NANOS));
        return rtn;
    }
    public static List<Integer> toIntList3(List<String> data) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        List<Integer> rtn = data.parallelStream()
                .map(Integer::valueOf)
                .collect(Collectors.toCollection(() -> Lists.newArrayListWithCapacity(data.size())));
        Duration duration = stopwatch.elapsed();
        System.out.println(duration.get(NANOS));
        return rtn;
    }
}
